package com.taobao.pet.controller;
import com.taobao.basic.exception.BusinessException;
import com.taobao.basic.util.JsonResult;
import com.taobao.basic.util.LoginContext;
import com.taobao.basic.util.PageList;
import com.taobao.pet.domain.SearchMasterMsg;
import com.taobao.pet.dto.AcceptDto;
import com.taobao.pet.query.SearchMasterMsgQuery;
import com.taobao.pet.service.ISearchMasterMsgService;
import com.taobao.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/searchMasterMsg")
@Api(value = "寻主接口类",description = "寻主接口的CRUD操作")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService SearchMasterMsgService;

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<SearchMasterMsg> findAll(HttpServletRequest request){

        // 获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);

        return SearchMasterMsgService.loadAll();
    }
    /**
     * 根据id查询单个数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public SearchMasterMsg findOne(@PathVariable("id") Long id){

        return SearchMasterMsgService.loadById(id);
    };
    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public JsonResult DeleteByID(@PathVariable("id") Long id){
        try {
            SearchMasterMsgService.remove(id);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
    /**
     * 添加或修改操作
     * @param SearchMasterMsg
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public JsonResult updateOrAdd(@RequestBody SearchMasterMsg SearchMasterMsg ){
        try {
            if (SearchMasterMsg.getId() == null){
                SearchMasterMsgService.save(SearchMasterMsg);
            }else {
                SearchMasterMsgService.update(SearchMasterMsg);
            }
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     *
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    // @RequestBody -从请求体中获取数据赋值给SearchMasterMsgQuery
    public PageList<SearchMasterMsg> pageQuery(@RequestBody SearchMasterMsgQuery query){
        try {
            return SearchMasterMsgService.pageQuery(query);
        }catch (Exception e){
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }
    /**
     * 批量删除
     * @param
     * @return
    */
    @PatchMapping()
    @ApiOperation(value = "删除接口")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            SearchMasterMsgService.patchDel(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }

    /**
     * 寻主
     * @param query
     * @return
     *
     */
    @PostMapping("/publish")
    @ApiOperation(value = "寻主")
    // @RequestBody -从请求体中获取数据赋值给SearchMasterMsgQuery
    public JsonResult publish(@RequestBody SearchMasterMsg smm,HttpServletRequest request){
        try {
            // 获取当前登陆人信息
            Logininfo logininfo = LoginContext.getLogininfo(request);
            SearchMasterMsgService.publish(smm,logininfo.getId());
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙！");
        }
    }
    /**
     * 待处理
     * @param query
     * @return
     *
     */
    @PostMapping("/toHandle")
    @ApiOperation(value = "分页查询接口")
    // @RequestBody -从请求体中获取数据赋值给SearchMasterMsgQuery
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            // 获取登录人信息
            Logininfo logininfo = LoginContext.getLogininfo(request);

            return SearchMasterMsgService.toHandle(query,logininfo.getId());
        }catch (Exception e){
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 用户
     * @param query
     * @return
     *
     */
    @PostMapping("/user")
    @ApiOperation(value = "分页查询接口")
    // @RequestBody -从请求体中获取数据赋值给SearchMasterMsgQuery
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            // 获取登录人信息
            Logininfo logininfo = LoginContext.getLogininfo(request);

            return SearchMasterMsgService.user(query,logininfo.getId());
        }catch (Exception e){
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 拒单
     * @param id
     * @return
     */
    @GetMapping("/reject/{id}")
    @ApiOperation(value = "拒单")
    public JsonResult reject(@PathVariable("id") Long id){
        try {
            SearchMasterMsgService.reject(id);
            return JsonResult.me();
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙！");
        }
    }
    /**
     * 接单
     * @param
     * @return
     */
    @PostMapping("/accept")
    @ApiOperation(value = "接单" )
    public JsonResult accept(@RequestBody AcceptDto dto, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            SearchMasterMsgService.accept(dto,logininfo.getId());
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

}
